// store.js
import { observable, action } from 'mobx-miniprogram'
import Tim from '../model/tim'
import TIM from 'tim-wx-sdk-ws'
export const store = observable({

  // 数据字段
  sdkReady: false,
  messageList:[],
  _targetUserId:null,
  
  // actions
  login: action(function () {
    this._runListener()
     Tim.getInstance().login()
  }),

  logout:action(function(){
    Tim.getInstance().logout()
  }),
  
  _runListener(){
    const sdk = Tim.getInstance().getSDK();
    sdk.on(TIM.EVENT.SDK_READY,this._handleSDKReady,this)
    sdk.on(TIM.EVENT.SDK_NOT_READY,this._handleSDKNotReady,this)
    sdk.on(TIM.EVENT.KICKED_OUT,this._handleSDKNotReady,this)
    // 接收消息
    sdk.on(TIM.EVENT.MESSAGE_RECEIVED,this._handleMessageReceived,this)
  },

  getMessageList:action(async function(){
    if(!this._targetUserId){
      throw Error('未指定目标用户id')
    }
    this.messageList = await Tim.getInstance().reset().getMessageList(this._targetUserId)
    await Tim.getInstance().setMessageRead(this._targetUserId)
  }),

  async _handleMessageReceived(event){
    console.log(event.data);
    if(!this._targetUserId){
      return 
    }

    const currentConversationMessage = event.data.filter(item => item.form === this._targetUserId )

    if(currentConversationMessage.length){
      this.messageList = this.messageList.concat(currentConversationMessage)
      await Tim.getInstance().setMessageRead(this._targetUserId)
    }

  },

  //设置目标用户id
  setTargetUserId:action(function(targetUserId){
    this._targetUserId = targetUserId
  }),

  _handleSDKReady(){
    this.sdkReady = true
  },

  _handleSDKNotReady(){
    this.sdkReady = false
  }

})